<template>
  <div class="app-container">
<!--查询模块-->
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="指定可看" prop="userId">
        <el-input
          v-model="queryParams.userId"
          clearable
          placeholder="请输入指定可看"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="相关目标" prop="goalsName">
        <el-input
          v-model="queryParams.goalsName"
          clearable
          placeholder="请输入相关目标"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日记标题" prop="title">
        <el-input
          v-model="queryParams.title"
          clearable
          placeholder="请输入日记标题"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当时心情" prop="mood">
        <el-input
          v-model="queryParams.mood"
          clearable
          placeholder="请输入当时心情"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="天气情况" prop="weather">
        <el-input
          v-model="queryParams.weather"
          clearable
          placeholder="请输入天气情况"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地理位置" prop="location">
        <el-input
          v-model="queryParams.location"
          clearable
          placeholder="请输入地理位置"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="纪念日提醒日期" prop="reminderDate">
        <el-date-picker v-model="queryParams.reminderDate"
                        clearable
                        placeholder="请选择纪念日提醒日期"
                        type="date"
                        value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
<!--添加按钮-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['base:entries:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['base:entries:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="mini"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['base:entries:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['base:entries:export']"
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
<!--日志列表详情-->
    <el-row :gutter="20">
      <el-col v-for="item in entriesList" :span="4">
        <el-card>
          <el-carousel :height=" windowHeight * 0.33+'px'" >
            <el-carousel-item v-for="(ing, index) in item.imagePaths" :key="item.entryId+index">
              <img :src="ing" class="image" style="border-radius: 10px 10px ; width:100%;height:100%">
            </el-carousel-item>
          </el-carousel>
          <div style="padding: 14px;">
            <div style="display: flex; justify-content: space-between; padding: 14px;">
              <span>{{ item.title }}</span>
              <span>{{ parseTime(item.createTime, '{y}-{m}-{d} {h}:{i} ') }}</span>
            </div>
            <div class="bottom clearfix">
              <el-button
                v-hasPermi="['base:entries:query']"
                icon="el-icon-edit"
                size="mini"
                type="text"
                @click="getInfo(item)"
              >详情
              </el-button>
              <el-button
                v-hasPermi="['base:entries:edit']"
                icon="el-icon-edit"
                size="mini"
                type="text"
                @click="handleUpdate(item)"
              >修改
              </el-button>
              <el-button
                v-hasPermi="['base:entries:remove']"
                icon="el-icon-delete"
                size="mini"
                type="text"
                @click="handleDelete(item)"
              >删除
              </el-button>
            </div>
          </div>


        </el-card>

      </el-col>

    </el-row>




    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

<!--    添加或修改对话框-->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="60%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="指定可看" prop="userId">
              <el-select v-model="form.userId" multiple placeholder="请选择指定可看">
                <el-option
                  v-for="item in userOptions"
                  :key="item.userId"
                  :label="item.nickName"
                  :value="item.userId"
                ></el-option>
              </el-select>
            </el-form-item>



          </el-col>
          <el-col :span="8">
            <el-form-item label="关联目标" prop="goalsModelId">
              <el-select v-model="form.goalsModelId" multiple placeholder="请选择关联的目标">
                <el-option
                  v-for="item in goalsModelOptions"
                  :key="item.id"
                  :label="item.goalName"
                  :value="parseInt(item.id)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="日记标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入日记标题"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="当时心情" prop="mood">
              <el-input v-model="form.mood" placeholder="请输入当时心情"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="天气情况" prop="weather">
              <el-input v-model="form.weather" placeholder="请输入天气情况"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="地理位置" prop="location">
              <el-input v-model="form.location" placeholder="请输入地理位置"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="纪念日提醒日期" prop="reminderDate">
              <el-date-picker v-model="form.reminderDate"
                              clearable
                              placeholder="请选择纪念日提醒日期"
                              type="date"
                              value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-switch
              v-model="isShow"
              active-text="图片上传"
              inactive-text="视频上传">
            </el-switch>
          </el-col>
          <el-col :span="24">

            <el-form-item label="请上传图片" prop="imagePaths"  v-if="isShow">
              <image-upload v-model="form.imagePaths" :limit="9"  @on-exceed="handleExceed"/>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="!isShow">
            <el-form-item label="请上传视频" prop="videoPaths" >
              <file-upload v-model="form.videoPaths" :fileSize="500" :fileType="['mp4', 'avi', 'mkv', 'mov', 'wmv', 'flv', 'webm', 'ogg', 'ts', 'asf', 'rmvb']"
                           :limit="9"
                           @on-exceed="handleExceed"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="富文本">
              <editor v-model="form.richContent" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入内容" type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-drawer size="50%"
      :title="title"
      :visible.sync="openInfo"
      :direction="direction"
      >

      <el-row gutter="20px" >
<!--        <el-col :span="24">-->
<!--          <div class="gap" style="width: 700px">-->
<!--          <el-carousel :height=" windowHeight * 0.6+'px'" >-->
<!--            <el-carousel-item v-for="(ing,index) in form.imagePaths" :key="index" >-->
<!--              <img :src="ing" class="image" style="width: 100%;height: 100%;border-radius: 10px 10px">-->
<!--            </el-carousel-item>-->
<!--          </el-carousel>-->
<!--          </div>-->
<!--        </el-col>-->
        <!-- 图片展示 -->
        <el-col :span="24" v-if="form.imagePaths && form.imagePaths.length > 0">
          <div class="gap" style="width: 700px">
            <el-carousel :height="windowHeight * 0.6 + 'px'">
              <el-carousel-item v-for="(filePath, index) in form.imagePaths" :key="index">
                <img :src="filePath" style="width: 100%; height: 100%; border-radius: 10px;" />
              </el-carousel-item>
            </el-carousel>
          </div>
        </el-col>

        <!-- 视频展示 -->
        <el-col :span="24" v-if="form.videoPaths && form.videoPaths.length > 0">
          <div class="gap" style="width: 700px; padding: 10px;">
            <el-carousel :height="windowHeight * 0.8 + 'px'">
              <el-carousel-item v-for="(filePath, index) in form.videoPaths" :key="index">
                <video :src="filePath" controls style="width: 100%; border-radius: 10px;"/>
              </el-carousel-item>
            </el-carousel>

          </div>
        </el-col>
        <el-col :span="24"><div class="gap"> <div style="display: flex; justify-content: space-between; align-items: center; padding: 10px;">
          <div>
            <span style="font-weight: bold;padding-left: 20px">天气情况:</span>
            <span style="padding-left: 20px">{{ form.weather }}</span>
          </div>
          <div>
            <span style="font-weight: bold;padding-right: 20px">地理位置:</span>
            <span style="padding-right: 20px" v-text="form.location"></span>
          </div>
        </div></div></el-col>
        <el-col :span="24"><div class="gap">  <div v-html="form.richContent"></div></div></el-col>
      </el-row>

    </el-drawer>
  </div>
</template>

<script>
import {listEntries, getEntries, delEntries, addEntries, updateEntries} from "@/api/base/entries";
import {listUser} from "@/api/system/user";
import {listModel} from "@/api/system/model";
import {isExternal} from "@/utils/validate";

export default {
  name: "Entries",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日记条目主表格数据
      entriesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      openInfo: false,
      // 备注时间范围
      daterangeCreateTime: [],
      // 备注时间范围
      daterangeUpdateTime: [],
      // 窗口的高度
      windowHeight: window.innerHeight,
      //图片或视频
      isShow: false,
     // 抽屉对话框

      direction: 'rtl',
      // 用户选项
      userOptions: [],
      //目标选择
      goalsModelOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        goalsModelId: null,
        goalsName: null,
        title: null,
        content: null,
        richContent: null,
        entryType: null,
        mood: null,
        weather: null,
        location: null,
        isPrivate: null,
        isFavorite: null,
        hasReminder: null,
        reminderDate: null,
      },

      // 表单参数
      form: {
        goalsModelId: [],
      },
      // 表单校验
      rules: {
        userId: [
          {required: true, message: "指定可看不能为空", trigger: "blur"}
        ],
        goalsName: [
          {required: true, message: "相关目标不能为空", trigger: "blur"}
        ],
        title: [
          {required: true, message: "日记标题不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getGoalsModelList();
    this.getUserList();

  },
  methods: {
    /** 查询日记条目主列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeUpdateTime && '' != this.daterangeUpdateTime) {
        this.queryParams.params["beginUpdateTime"] = this.daterangeUpdateTime[0];
        this.queryParams.params["endUpdateTime"] = this.daterangeUpdateTime[1];
      }
      listEntries(this.queryParams).then(response => {
        this.entriesList = response.rows;
        //拼接图片
        this.entriesList.forEach(item => {
          let real_src_list = item.imagePaths.split(",");
          let srcList = [];
          real_src_list.forEach(item => {
            return srcList.push(process.env.VUE_APP_BASE_API + item);
          });
          item.imagePaths = srcList;

        });
        this.total = response.total;
        this.loading = false;
        console.log(this.entriesList)
      });
    },

    //查询指定可看的用户
    getUserList() {
      listUser({pageNum: 1, pageSize: 10000}).then(response => {
        this.userOptions = response.rows;
        console.log(this.userOptions)
      });
    },
    /** 获取目标模板列表 */
    getGoalsModelList() {
      listModel({pageNum: 1, pageSize: 10000}).then(response => {
        this.goalsModelOptions = response.rows;
        console.log(this.goalsModelOptions)
      });
    },
    //详情
    getInfo(entries) {

      this.reset();
      const entryId = entries.entryId
      getEntries(entryId).then(response => {
        console.log('77777',response)

        this.form = response.data;
        if (this.form.imagePaths != null) {
          let real_src_list = this.form.imagePaths.split(",");
          let srcList = [];
          real_src_list.forEach(item => {
            return srcList.push(process.env.VUE_APP_BASE_API + item);
          });
          this.form.imagePaths = srcList
          console.log(this.form.imagePaths)
        } else {
          this.form.imagePaths = [];
        }

        if (this.form.videoPaths != null) {
          let real_src_list = this.form.videoPaths.split(",");
          let srcList = [];
          real_src_list.forEach(item => {
            return srcList.push(process.env.VUE_APP_BASE_API + item);
          });
          this.form.videoPaths = srcList
          console.log(this.form.videoPaths)
        } else {
          this.form.videoPaths = [];
        }
        this.openInfo=true;
        this.title =  this.form.title+"详情";
      });

      this.openInfo = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        entryId: null,
        userId: null,
        goalsModelId: [],
        goalsName: null,
        title: null,
        content: null,
        richContent: null,
        entryType: null,
        mood: null,
        weather: null,
        location: null,
        isPrivate: null,
        isFavorite: null,
        hasReminder: null,
        reminderDate: null,
        imagePaths: null,
        videoPaths: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeUpdateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.entryId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();

      this.open = true;
      this.title = "添加日记条目主";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

      const entryId = row.entryId || this.ids
      getEntries(entryId).then(response => {
        this.form = response.data;
        if (null != this.form.goalsModelId) {
          this.form.goalsModelId = this.form.goalsModelId.split(',').map(Number);
        }
         if(null != this.form.userId) {
          this.form.userId = this.form.userId.split(',').map(Number);
        }
        console.log("this.form", this.form)
        this.open = true;
        this.title = "修改日记条目主";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {

          // 将 userIds 数组转换为逗号分隔的字符串
          this.form.goalsModelId = this.form.goalsModelId.join(',');
          this.form.userId = this.form.userId.join(',');

          if (this.form.entryId != null) {
            updateEntries(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEntries(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const entryIds = row.entryId || this.ids;
      this.$modal.confirm('是否确认删除日记条目主编号为"' + entryIds + '"的数据项？').then(function () {
        return delEntries(entryIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/entries/export', {
        ...this.queryParams
      }, `entries_${new Date().getTime()}.xlsx`)
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 9 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    }
  }
};
</script>
<style>
.gap{
  margin-top: 20px;
  margin-left: 200px;
  margin-right: 30px;
}
</style>
